{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "view-in-github"
   },
   "source": [
    "<a href=\"https://colab.research.google.com/github/NeoNeuron/comp-neuro-course/blob/master/tutorials/W0_PythonWorkshop1/W0D1_Tutorial1.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "# Python Workshop - LIF Neuron - Part I\n",
    "\n",
    "__Original Content creators:__ Marco Brigham and the [CCNSS](https://www.ccnss.org/) team\n",
    "\n",
    "__Content Modified:__ Kai Chen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "---\n",
    "## Tutorial objectives\n",
    "You are going to use Python skills to advance your understanding of neuroscience. Just like two legs that support and strengthen each other. One has \"Python\" written in it, and the other has \"Neuro\". And step-by-step they go.\n",
    "\n",
    "&nbsp; \n",
    "\n",
    "In this notebook, we'll practice basic operations with Python variables, control flow, plotting, and a sneak peek at `np.array`, the workhorse of scientific computation in Python.\n",
    "\n",
    "&nbsp; \n",
    "\n",
    "Each new concept in Python will unlock a different aspect of our implementation of a **Leaky Integrate-and-Fire (LIF)** neuron. And as if it couldn't get any better, we'll visualize the evolution of its membrane potential in time, and extract its statistical properties!\n",
    "\n",
    "&nbsp; \n",
    "\n",
    "Well then, let's start our walk today!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "---\n",
    "## Imports and helper functions\n",
    "Please execute the cell(s) below to initialize the notebook environment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cellView": "both",
    "colab": {},
    "colab_type": "code",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:09.389507Z",
     "iopub.status.busy": "2020-10-15T15:23:09.388769Z",
     "iopub.status.idle": "2020-10-15T15:23:09.683506Z",
     "shell.execute_reply": "2020-10-15T15:23:09.683957Z"
    }
   },
   "outputs": [],
   "source": [
    "# Import libraries\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cellView": "form",
    "colab": {},
    "colab_type": "code",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:09.694030Z",
     "iopub.status.busy": "2020-10-15T15:23:09.693460Z",
     "iopub.status.idle": "2020-10-15T15:23:10.856423Z",
     "shell.execute_reply": "2020-10-15T15:23:10.857016Z"
    }
   },
   "outputs": [],
   "source": [
    "# @title Figure settings\n",
    "\n",
    "%config InlineBackend.figure_format = 'retina'\n",
    "\n",
    "plt.style.use(\"https://raw.githubusercontent.com/NeoNeuron/professional-workshop-3/master/nma.mplstyle\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "---\n",
    "## Neuron model\n",
    "A *membrane equation* and a *reset condition* define our *leaky-integrate-and-fire (LIF)* neuron:\n",
    "\n",
    "\n",
    "\\begin{align*}\n",
    "\\\\\n",
    "&\\tau_m\\,\\frac{d}{dt}\\,V(t) = E_{L} - V(t) + R\\,I(t) &\\text{if }\\quad V(t) \\leq V_{th}\\\\\n",
    "\\\\\n",
    "&V(t) = V_{reset} &\\text{otherwise}\\\\\n",
    "\\\\\n",
    "\\end{align*}\n",
    "\n",
    "where $V(t)$ is the membrane potential, $\\tau_m$ is the membrane time constant, $E_{L}$ is the leak potential, $R$ is the membrane resistance, $I(t)$ is the synaptic input current, $V_{th}$ is the firing threshold, and $V_{reset}$ is the reset voltage. We can also write $V_m$ for membrane potential - very convenient for plot labels.\n",
    "\n",
    "The membrane equation is an *ordinary differential equation (ODE)* that describes the time evolution of membrane potential $V(t)$ in response to synaptic input and leaking of change across the cell membrane.\n",
    "\n",
    "**Note that, in this tutorial the neuron model will not implement a spiking mechanism.**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "### Exercise 1\n",
    "We start by defining and initializing the main simulation variables.\n",
    "\n",
    "**Suggestions**\n",
    "* Modify the code below to print the simulation parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:10.861625Z",
     "iopub.status.busy": "2020-10-15T15:23:10.860827Z",
     "iopub.status.idle": "2020-10-15T15:23:10.863473Z",
     "shell.execute_reply": "2020-10-15T15:23:10.864187Z"
    }
   },
   "outputs": [],
   "source": [
    "# t_max = 150e-3   # second\n",
    "# dt = 1e-3        # second\n",
    "# tau = 20e-3      # second\n",
    "# el = -60e-3      # milivolt\n",
    "# vr = -70e-3      # milivolt\n",
    "# vth = -50e-3     # milivolt\n",
    "# r = 100e6        # ohm\n",
    "# i_mean = 25e-11  # ampere\n",
    "\n",
    "# print(t_max, dt, tau, el, vr, vth, r, i_mean)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "**SAMPLE OUTPUT**\n",
    "\n",
    "```\n",
    "0.15 0.001 0.02 -0.06 -0.07 -0.05 100000000.0 2.5e-10\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cellView": "code",
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 34
    },
    "colab_type": "text",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:10.869891Z",
     "iopub.status.busy": "2020-10-15T15:23:10.869070Z",
     "iopub.status.idle": "2020-10-15T15:23:10.872390Z",
     "shell.execute_reply": "2020-10-15T15:23:10.872874Z"
    },
    "outputId": "df8e8379-5ed2-482a-b4e0-97117625ba28"
   },
   "source": [
    "[*Click for solution*](https://github.com/NeoNeuron/professional-workshop-3/tree/master//tutorials/W0_PythonWorkshop/solutions/W0_Tutorial1_Solution_4adeccd3.py)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "### Exercise 2\n",
    "![synaptic input](https://github.com/mpbrigham/colaboratory-figures/raw/master/nma/python-for-nma/synaptic_input.png)\n",
    "\n",
    "We start with a sinusoidal model to simulate the synaptic input $I(t)$ given by:\n",
    "\\begin{align*}\n",
    "\\\\\n",
    "I(t)=I_{mean}\\left(1+\\sin\\left(\\frac{2 \\pi}{0.01}\\,t\\right)\\right)\\\\\n",
    "\\\\\n",
    "\\end{align*}\n",
    "\n",
    "Compute the values of synaptic input $I(t)$ between $t=0$ and $t=0.009$ with step $\\Delta t=0.001$.\n",
    "\n",
    "**Suggestions**\n",
    "* Loop variable `step` for 10 steps (`step` takes values from `0` to `9`)\n",
    "* At each time step\n",
    "    * Compute the value of `t` with variables `step` and `dt`\n",
    "    * Compute the value of `i`\n",
    "    * Print `i`\n",
    "* Use `np.pi` and `np.sin` for evaluating $\\pi$ and $\\sin(\\cdot)$, respectively"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 191
    },
    "colab_type": "code",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:10.877084Z",
     "iopub.status.busy": "2020-10-15T15:23:10.876500Z",
     "iopub.status.idle": "2020-10-15T15:23:10.879044Z",
     "shell.execute_reply": "2020-10-15T15:23:10.878600Z"
    },
    "outputId": "9d4f454a-91e6-44f9-d2bd-2dabce3b7073"
   },
   "outputs": [],
   "source": [
    "# initialize t\n",
    "t = 0\n",
    "\n",
    "# loop for 10 steps, variable 'step' takes values from 0 to 9\n",
    "for step in range(10):\n",
    "  t = step * dt\n",
    "  i = ...\n",
    "  print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "**SAMPLE OUTPUT**\n",
    "\n",
    "```\n",
    "2.5e-10\n",
    "3.969463130731183e-10\n",
    "4.877641290737885e-10\n",
    "4.877641290737885e-10\n",
    "3.9694631307311837e-10\n",
    "2.5000000000000007e-10\n",
    "1.0305368692688176e-10\n",
    "1.2235870926211617e-11\n",
    "1.223587092621159e-11\n",
    "1.0305368692688186e-10\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 191
    },
    "colab_type": "text",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:10.883180Z",
     "iopub.status.busy": "2020-10-15T15:23:10.882587Z",
     "iopub.status.idle": "2020-10-15T15:23:10.884773Z",
     "shell.execute_reply": "2020-10-15T15:23:10.885178Z"
    },
    "outputId": "470871d7-8d78-4057-8edb-c74d294a950f"
   },
   "source": [
    "[*Click for solution*](https://github.com/NeoNeuron/professional-workshop-3/tree/master//tutorials/W0_PythonWorkshop/solutions/W0_Tutorial1_Solution_943bc60a.py)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "### Exercise 3\n",
    "Print formatting is handy for displaying simulation parameters in a clean and organized form. Python 3.6 introduced the new string formatting [f-strings](https://www.python.org/dev/peps/pep-0498). Since we are dealing with type `float` variables, we use `f'{x:.3f}'` for formatting `x` to three decimal points, and `f'{x:.4e}'` for four decimal points but in exponential notation.\n",
    "```\n",
    "x = 3.14159265e-1\n",
    "print(f'{x:.3f}')\n",
    "--> 0.314\n",
    "\n",
    "print(f'{x:.4e}')\n",
    "--> 3.1416e-01\n",
    "```\n",
    "\n",
    "Repeat the loop from the previous exercise and print the `t` values with three decimal points, and synaptic input $I(t)$ with four decimal points in exponential notation.\n",
    "\n",
    "For additional formatting options with f-strings see [here](http://zetcode.com/python/fstring/).\n",
    "\n",
    "**Suggestions**\n",
    "* Print `t` and `i` with help of *f-strings* formatting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 191
    },
    "colab_type": "code",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:10.888632Z",
     "iopub.status.busy": "2020-10-15T15:23:10.888099Z",
     "iopub.status.idle": "2020-10-15T15:23:10.890162Z",
     "shell.execute_reply": "2020-10-15T15:23:10.890546Z"
    },
    "outputId": "f3abcb8b-7c17-44cc-cda3-8aad5ac37433"
   },
   "outputs": [],
   "source": [
    "# initialize step_end\n",
    "step_end = 10\n",
    "\n",
    "# loop for step_end steps\n",
    "for step in range(step_end):\n",
    "  t = step * dt\n",
    "  i = ...\n",
    "  print(...)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "**SAMPLE OUTPUT**\n",
    "\n",
    "```\n",
    "0.000 2.5000e-10\n",
    "0.001 3.9695e-10\n",
    "0.002 4.8776e-10\n",
    "0.003 4.8776e-10\n",
    "0.004 3.9695e-10\n",
    "0.005 2.5000e-10\n",
    "0.006 1.0305e-10\n",
    "0.007 1.2236e-11\n",
    "0.008 1.2236e-11\n",
    "0.009 1.0305e-10\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 191
    },
    "colab_type": "text",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:10.894414Z",
     "iopub.status.busy": "2020-10-15T15:23:10.893855Z",
     "iopub.status.idle": "2020-10-15T15:23:10.895789Z",
     "shell.execute_reply": "2020-10-15T15:23:10.896144Z"
    },
    "outputId": "286713fa-b240-4c38-b8d4-1d87a2b7379f"
   },
   "source": [
    "[*Click for solution*](https://github.com/NeoNeuron/professional-workshop-3/tree/master//tutorials/W0_PythonWorkshop/solutions/W0_Tutorial1_Solution_cae53962.py)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "## ODE integration without spikes\n",
    "In the next exercises, we now simulate the evolution of the membrane equation in discrete time steps, with a sufficiently small $\\Delta t$.\n",
    "\n",
    "We start by writing the time derivative $d/dt\\,V(t)$ in the membrane equation without taking the limit $\\Delta t \\to 0$:\n",
    "\n",
    "\\begin{align*}\n",
    "\\\\\n",
    "\\tau_m\\,\\frac{V\\left(t+\\Delta t\\right)-V\\left(t\\right)}{\\Delta t} &= E_{L} - V(t) + R\\,I(t) \\qquad\\qquad (1)\\\\\n",
    "\\\\\n",
    "\\end{align*}\n",
    "\n",
    "The value of membrane potential $V\\left(t+\\Delta t\\right)$ can be expressed in terms of its previous value $V(t)$ by simple algebraic manipulation. For *small enough* values of $\\Delta t$, this provides a good approximation of the continuous-time integration.\n",
    "\n",
    "This operation is an integration since we obtain a sequence $\\{V(t), V(t+\\Delta t), V(t+2\\Delta t),...\\}$ starting from the ODE. Notice how the ODE describes the evolution of $\\frac{d}{dt}\\,V(t)$, the derivative of $V(t)$, but not directly the evolution of $V(t)$. For the evolution of $V(t)$ we need to integrate the ODE, and in this tutorial, we will do a discrete-time integration using the Euler method. See [Numerical methods for ordinary differential equations](https://en.wikipedia.org/wiki/Numerical_methods_for_ordinary_differential_equations) for additional details."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "### Exercise 4\n",
    "Compute the values of $V(t)$ between $t=0$ and $t=0.01$ with step $\\Delta t=0.001$ and $V(0)=E_L$.\n",
    "\n",
    "We will write a `for` loop from scratch in this exercise. The following three formulations are all equivalent and loop for three steps:\n",
    "```\n",
    "for step in [0, 1, 2]:\n",
    "  print(step)\n",
    "\n",
    "for step in range(3):\n",
    "  print(step)\n",
    "\n",
    "start = 0\n",
    "end = 3\n",
    "stepsize = 1\n",
    "\n",
    "for step in range(start, end, stepsize):\n",
    "  print(step)\n",
    "```\n",
    "\n",
    "\n",
    "**Suggestions**\n",
    "* Reorganize the Eq. (1) to isolate $V\\left(t+\\Delta t\\right)$ on the left side, and express it as function of $V(t)$ and the other terms\n",
    "* Initialize the membrane potential variable `v` to leak potential `el`\n",
    "* Loop variable `step` for `10` steps\n",
    "* At each time step\n",
    "    * Compute the current value of `t`, `i`\n",
    "    * Print the current value of `t` and `v`\n",
    "    * Update the value of `v`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 191
    },
    "colab_type": "code",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:10.899628Z",
     "iopub.status.busy": "2020-10-15T15:23:10.899089Z",
     "iopub.status.idle": "2020-10-15T15:23:10.900888Z",
     "shell.execute_reply": "2020-10-15T15:23:10.901267Z"
    },
    "outputId": "7f930e78-ed89-48b9-9c01-e8778c63d48d"
   },
   "outputs": [],
   "source": [
    "# initialize step_end and v\n",
    "step_end = 10\n",
    "v = el\n",
    "\n",
    "# loop for step_end steps\n",
    "for step in range(step_end):\n",
    "  t = step * dt\n",
    "  i = ...\n",
    "  print(...)\n",
    "  v = ..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "**SAMPLE OUTPUT**\n",
    "\n",
    "```\n",
    "0.000 -6.0000e-02\n",
    "0.001 -5.8750e-02\n",
    "0.002 -5.6828e-02\n",
    "0.003 -5.4548e-02\n",
    "0.004 -5.2381e-02\n",
    "0.005 -5.0778e-02\n",
    "0.006 -4.9989e-02\n",
    "0.007 -4.9974e-02\n",
    "0.008 -5.0414e-02\n",
    "0.009 -5.0832e-02\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 191
    },
    "colab_type": "text",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:10.905216Z",
     "iopub.status.busy": "2020-10-15T15:23:10.904715Z",
     "iopub.status.idle": "2020-10-15T15:23:10.906608Z",
     "shell.execute_reply": "2020-10-15T15:23:10.906966Z"
    },
    "outputId": "6999b084-1660-4936-e3e4-fa5873abd208"
   },
   "source": [
    "[*Click for solution*](https://github.com/NeoNeuron/professional-workshop-3/tree/master//tutorials/W0_PythonWorkshop/solutions/W0_Tutorial1_Solution_95c91766.py)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "### Exercise 5\n",
    "![synaptic input discrete](https://github.com/mpbrigham/colaboratory-figures/raw/master/nma/python-for-nma/synaptic_input_discrete.png)\n",
    "\n",
    "Plot the values of $I(t)$ between $t=0$ and $t=0.024$.\n",
    "\n",
    "**Suggestions**\n",
    "* Increase `step_end`\n",
    "* initialize the figure with `plt.figure`, set title, x and y labels with `plt.title`, `plt.xlabel` and `plt.ylabel`, respectively\n",
    "* Replace printing command `print` with plotting command `plt.plot` with argument `'ko'` (short version for `color='k'` and `marker='o'`) for black small dots\n",
    "* Use `plt.show()` at the end to display the plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 34
    },
    "colab_type": "code",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:10.910203Z",
     "iopub.status.busy": "2020-10-15T15:23:10.909690Z",
     "iopub.status.idle": "2020-10-15T15:23:10.916390Z",
     "shell.execute_reply": "2020-10-15T15:23:10.916800Z"
    },
    "outputId": "a503a18a-88c4-4086-e912-973215e084d6"
   },
   "outputs": [],
   "source": [
    "# initialize step_end\n",
    "step_end = 25\n",
    "\n",
    "# initialize the figure\n",
    "plt.figure()\n",
    "# Complete these lines and uncomment\n",
    "# plt.title(...)\n",
    "# plt.xlabel(...)\n",
    "# plt.ylabel(...)\n",
    "\n",
    "# loop for step_end steps\n",
    "for step in range(step_end):\n",
    "  t = step * dt\n",
    "  i = ...\n",
    "  # Complete this line and uncomment\n",
    "  # plt.plot(...)\n",
    "\n",
    "# plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 433
    },
    "colab_type": "text",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:10.920979Z",
     "iopub.status.busy": "2020-10-15T15:23:10.920478Z",
     "iopub.status.idle": "2020-10-15T15:23:11.311590Z",
     "shell.execute_reply": "2020-10-15T15:23:11.312020Z"
    },
    "outputId": "f183ce0a-6b84-4d27-e2af-28831afede92"
   },
   "source": [
    "[*Click for solution*](https://github.com/NeoNeuron/professional-workshop-3/tree/master//tutorials/W0_PythonWorkshop/solutions/W0_Tutorial1_Solution_23446a7e.py)\n",
    "\n",
    "*Example output:*\n",
    "\n",
    "<img alt='Solution hint' align='left' width=559 height=416 src=https://raw.githubusercontent.com/NeoNeuron/professional-workshop-3/master/tutorials/W0_PythonWorkshop/static/W0_Tutorial1_Solution_23446a7e_0.png>\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "### Exercise 6\n",
    "Plot the values of $V(t)$ between $t=0$ and $t=t_{max}$.\n",
    "\n",
    "**Suggestions**\n",
    "* Compute the required number of steps with`int(t_max/dt)`\n",
    "* Use plotting command for black small(er) dots with argument `'k.'`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 430
    },
    "colab_type": "code",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:11.329840Z",
     "iopub.status.busy": "2020-10-15T15:23:11.326980Z",
     "iopub.status.idle": "2020-10-15T15:23:11.451675Z",
     "shell.execute_reply": "2020-10-15T15:23:11.452033Z"
    },
    "outputId": "2a436d51-b48c-4cd6-c7f2-f8229b4b2fe1"
   },
   "outputs": [],
   "source": [
    "# initialize step_end and v\n",
    "step_end = int(t_max / dt)\n",
    "v = el\n",
    "\n",
    "# initialize the figure\n",
    "plt.figure()\n",
    "plt.title('$V_m$ with sinusoidal I(t)')\n",
    "plt.xlabel('time (s)')\n",
    "plt.ylabel('$V_m$ (V)');\n",
    "\n",
    "# loop for step_end steps\n",
    "for step in range(step_end):\n",
    "  t = step * dt\n",
    "  i = ...\n",
    "  # Complete this line and uncomment\n",
    "  # plt.plot(...)\n",
    "\n",
    "  v = ...\n",
    "\n",
    "t = t + dt\n",
    "# Complete this line and uncomment\n",
    "# plt.plot(...)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 433
    },
    "colab_type": "text",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:11.460584Z",
     "iopub.status.busy": "2020-10-15T15:23:11.460019Z",
     "iopub.status.idle": "2020-10-15T15:23:11.796719Z",
     "shell.execute_reply": "2020-10-15T15:23:11.797092Z"
    },
    "outputId": "4717b09a-a9aa-4236-e36a-785bbcf36277"
   },
   "source": [
    "[*Click for solution*](https://github.com/NeoNeuron/professional-workshop-3/tree/master//tutorials/W0_PythonWorkshop/solutions/W0_Tutorial1_Solution_1046fd94.py)\n",
    "\n",
    "*Example output:*\n",
    "\n",
    "<img alt='Solution hint' align='left' width=560 height=416 src=https://raw.githubusercontent.com/NeoNeuron/professional-workshop-3/master/tutorials/W0_PythonWorkshop/static/W0_Tutorial1_Solution_1046fd94_0.png>\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "---\n",
    "## Random synaptic input\n",
    "From the perspective of neurons, synaptic input is random (or stochastic). We'll improve the synaptic input model by introducing random input current with statistical properties similar to the previous exercise:\n",
    "\n",
    "\\begin{align*}\n",
    "\\\\\n",
    "I(t)=I_{mean}\\left(1+0.1\\sqrt{\\frac{t_{max}}{\\Delta t}}\\,\\xi(t)\\right)\\qquad\\text{with }\\xi(t)\\sim U(-1,1)\\\\\n",
    "\\\\\n",
    "\\end{align*}\n",
    "\n",
    "where $U(-1,1)$ is the [uniform distribution](https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)) with support $x\\in[-1,1]$.\n",
    "\n",
    "Random synaptic input $I(t)$ results in random time course for $V(t)$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "### Exercise 7\n",
    "Plot the values of $V(t)$ between $t=0$ and $t=t_{max}-\\Delta t$ with random input $I(t)$.\n",
    "\n",
    "Initialize the (pseudo) random number generator (RNG) to a fixed value to obtain the same random input each time.\n",
    "\n",
    "The function `np.random.seed()` initializes the RNG, and `np.random.random()` generates samples from the uniform distribution between `0` and `1`.\n",
    "\n",
    "**Suggestions**\n",
    "* Use `np.random.seed()` to initialize the RNG to `0`\n",
    "* Use `np.random.random()` to generate random input in range `[0,1]` at each timestep\n",
    "* Multiply random input by an appropriate factor to expand the range to `[-1,1]`\n",
    "* Verify that $V(t)$ has a random time course by changing the initial RNG value\n",
    "* Alternatively, comment RNG initialization by typing `CTRL` + `\\` in the relevant line"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 430
    },
    "colab_type": "code",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:11.813145Z",
     "iopub.status.busy": "2020-10-15T15:23:11.812337Z",
     "iopub.status.idle": "2020-10-15T15:23:11.929080Z",
     "shell.execute_reply": "2020-10-15T15:23:11.929452Z"
    },
    "outputId": "886f9c47-dd97-46e3-f01b-cbb3d8748b52"
   },
   "outputs": [],
   "source": [
    "# set random number generator\n",
    "np.random.seed(2020)\n",
    "\n",
    "# initialize step_end and v\n",
    "step_end = int(t_max / dt)\n",
    "v = el\n",
    "\n",
    "# initialize the figure\n",
    "plt.figure()\n",
    "plt.title('$V_m$ with random I(t)')\n",
    "plt.xlabel('time (s)')\n",
    "plt.ylabel('$V_m$ (V)')\n",
    "\n",
    "# loop for step_end steps\n",
    "for step in range(step_end):\n",
    "  t = step * dt\n",
    "  # Complete this line and uncomment\n",
    "  # plt.plot(...)\n",
    "\n",
    "  i = ...\n",
    "  v = ...\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 433
    },
    "colab_type": "text",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:11.945012Z",
     "iopub.status.busy": "2020-10-15T15:23:11.944475Z",
     "iopub.status.idle": "2020-10-15T15:23:12.318038Z",
     "shell.execute_reply": "2020-10-15T15:23:12.318397Z"
    },
    "outputId": "59008d18-20a6-4dd6-a087-573510c12053"
   },
   "source": [
    "[*Click for solution*](https://github.com/NeoNeuron/professional-workshop-3/tree/master//tutorials/W0_PythonWorkshop/solutions/W0_Tutorial1_Solution_c3654fed.py)\n",
    "\n",
    "*Example output:*\n",
    "\n",
    "<img alt='Solution hint' align='left' width=560 height=416 src=https://raw.githubusercontent.com/NeoNeuron/professional-workshop-3/master/tutorials/W0_PythonWorkshop/static/W0_Tutorial1_Solution_c3654fed_0.png>\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "## Ensemble statistics\n",
    "Multiple runs of the previous exercise may give the impression of periodic regularity in the evolution of $V(t)$. We'll collect the sample mean over $N=50$ realizations of $V(t)$  with random input to test such a hypothesis. The sample mean, sample variance and sample autocovariance at times $\\left\\{t, s\\right\\}\\in[0,t_{max}]$, and for $N$ realizations $V_n(t)$ are given by:\n",
    "\n",
    "\\begin{align*}\n",
    "\\\\\n",
    "\\left\\langle V(t)\\right\\rangle &= \\frac{1}{N}\\sum_{n=1}^N V_n(t) & & \\text{sample mean}\\\\\n",
    "\\left\\langle (V(t)-\\left\\langle V(t)\\right\\rangle)^2\\right\\rangle &= \\frac{1}{N-1} \\sum_{n=1}^N \\left(V_n(t)-\\left\\langle V(t)\\right\\rangle\\right)^2 & & \\text{sample variance} \\\\\n",
    "\\left\\langle \\left(V(t)-\\left\\langle V(t)\\right\\rangle\\right)\\left(V(s)-\\left\\langle V(s)\\right\\rangle\\right)\\right\\rangle\n",
    "&= \\frac{1}{N-1} \\sum_{n=1}^N \\left(V_n(t)-\\left\\langle V(t)\\right\\rangle\\right)\\left(V_n(s)-\\left\\langle V(s)\\right\\rangle\\right) & & \\text{sample autocovariance}\\\\\n",
    "\\\\\n",
    "\\end{align*}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "### Exercise 8\n",
    "Plot multiple realizations ($N=50$) of $V(t)$ by storing in a list the voltage of each neuron at time $t$.\n",
    "\n",
    "Keep in mind that the plotting command `plt.plot(x, y)` requires `x` to have the same number of elements as `y`.\n",
    "\n",
    "Mathematical symbols such as $\\alpha$ and $\\beta$ are specified as `$\\alpha$` and `$\\beta$` in [TeX markup](https://en.wikipedia.org/wiki/TeX). See additional details in [Writing mathematical expressions](https://matplotlib.org/3.2.2/tutorials/text/mathtext.html) in Matplotlib.\n",
    "\n",
    "**Suggestions**\n",
    "* Initialize a list `v_n` with `50` values of membrane leak potential `el`\n",
    "* At each time step:\n",
    "  * Plot `v_n` with argument `'k.'` and parameter `alpha=0.05` to adjust the transparency (by default, `alpha=1`)\n",
    "  * In the plot command, replace `t` from the previous exercises with a list of size `n` with values `t`\n",
    "  * Loop over `50` realizations of random input\n",
    "  * Update `v_n` with the values of $V(t)$\n",
    "\n",
    "* Why is there a black dot at $t=0$?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 430
    },
    "colab_type": "code",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:12.345092Z",
     "iopub.status.busy": "2020-10-15T15:23:12.330304Z",
     "iopub.status.idle": "2020-10-15T15:23:12.450559Z",
     "shell.execute_reply": "2020-10-15T15:23:12.450919Z"
    },
    "outputId": "17bababd-4b2f-49e5-c5a9-b9edd53aa7d7"
   },
   "outputs": [],
   "source": [
    "# set random number generator\n",
    "np.random.seed(2020)\n",
    "\n",
    "# initialize step_end, n and v_n\n",
    "step_end = int(t_max / dt)\n",
    "n = 50\n",
    "# Complete this line and uncomment\n",
    "# v_n = ...\n",
    "\n",
    "# initialize the figure\n",
    "plt.figure()\n",
    "plt.title('Multiple realizations of $V_m$')\n",
    "plt.xlabel('time (s)')\n",
    "plt.ylabel('$V_m$ (V)')\n",
    "\n",
    "# loop for step_end steps\n",
    "for step in range(step_end):\n",
    "  t = step * dt\n",
    "  # Complete this line and uncomment\n",
    "  # plt.plot(...)\n",
    "\n",
    "  # loop for n steps\n",
    "  for j in range(0, n):\n",
    "    i = ...\n",
    "    # Complete this line and uncomment\n",
    "    # v_n[j] = ...\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 430
    },
    "colab_type": "text",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:12.465801Z",
     "iopub.status.busy": "2020-10-15T15:23:12.465044Z",
     "iopub.status.idle": "2020-10-15T15:23:12.782475Z",
     "shell.execute_reply": "2020-10-15T15:23:12.782823Z"
    },
    "outputId": "7f9d3a7e-e823-4423-b88b-030f9a3880a2"
   },
   "source": [
    "[*Click for solution*](https://github.com/NeoNeuron/professional-workshop-3/tree/master//tutorials/W0_PythonWorkshop/solutions/W0_Tutorial1_Solution_8b55f5dd.py)\n",
    "\n",
    "*Example output:*\n",
    "\n",
    "<img alt='Solution hint' align='left' width=558 height=413 src=https://raw.githubusercontent.com/NeoNeuron/professional-workshop-3/master/tutorials/W0_PythonWorkshop/static/W0_Tutorial1_Solution_8b55f5dd_0.png>\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "### Exercise 9\n",
    "Add the sample mean $\\left\\langle V(t)\\right\\rangle=\\frac{1}{N}\\sum_{n=1}^N V_n(t)$ to the plot.\n",
    "\n",
    "**Suggestions**\n",
    "* At each timestep:\n",
    "  * Compute and store in `v_mean` the sample mean $\\left\\langle V(t)\\right\\rangle$ by summing the values of list `v_n` with `sum` and dividing by `n`\n",
    "  * Plot $\\left\\langle V(t)\\right\\rangle$ with `alpha=0.8` and argument `'C0.'` for blue (you can read more about [specifying colors](https://matplotlib.org/tutorials/colors/colors.html#sphx-glr-tutorials-colors-colors-py))\n",
    "  * Loop over `50` realizations of random input\n",
    "  * Update `v_n` with the values of $V(t)$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 430
    },
    "colab_type": "code",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:12.806735Z",
     "iopub.status.busy": "2020-10-15T15:23:12.806134Z",
     "iopub.status.idle": "2020-10-15T15:23:12.917129Z",
     "shell.execute_reply": "2020-10-15T15:23:12.917514Z"
    },
    "outputId": "74ac7577-28a2-4ac0-ad57-e1d6a8d121a9"
   },
   "outputs": [],
   "source": [
    "# set random number generator\n",
    "np.random.seed(2020)\n",
    "\n",
    "# initialize step_end, n and v_n\n",
    "step_end = int(t_max / dt)\n",
    "n = 50\n",
    "v_n = [el] * n\n",
    "\n",
    "# initialize the figure\n",
    "plt.figure()\n",
    "plt.title('Multiple realizations of $V_m$')\n",
    "plt.xlabel('time (s)')\n",
    "plt.ylabel('$V_m$ (V)')\n",
    "\n",
    "# loop for step_end steps\n",
    "for step in range(step_end):\n",
    "  t = step * dt\n",
    "\n",
    "  v_mean = ...\n",
    "  # Complete these lines and uncomment\n",
    "  # plt.plot(...)\n",
    "  # plt.plot(...)\n",
    "\n",
    "  for j in range(0, n):\n",
    "    i = ...\n",
    "    v_n[j] = ...\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 430
    },
    "colab_type": "text",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:12.934134Z",
     "iopub.status.busy": "2020-10-15T15:23:12.933609Z",
     "iopub.status.idle": "2020-10-15T15:23:13.390514Z",
     "shell.execute_reply": "2020-10-15T15:23:13.390872Z"
    },
    "outputId": "c3d56394-02ec-4eb9-8b4a-84095759f245"
   },
   "source": [
    "[*Click for solution*](https://github.com/NeoNeuron/professional-workshop-3/tree/master//tutorials/W0_PythonWorkshop/solutions/W0_Tutorial1_Solution_98017570.py)\n",
    "\n",
    "*Example output:*\n",
    "\n",
    "<img alt='Solution hint' align='left' width=558 height=413 src=https://raw.githubusercontent.com/NeoNeuron/professional-workshop-3/master/tutorials/W0_PythonWorkshop/static/W0_Tutorial1_Solution_98017570_0.png>\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "### Exercise 10\n",
    "Add the sample standard deviation $\\sigma(t)\\equiv\\sqrt{\\text{Var}\\left(t\\right)}$ to the plot, with sample variance $\\text{Var}(t) = \\frac{1}{N-1} \\sum_{n=1}^N \\left(V_n(t)-\\left\\langle V(t)\\right\\rangle\\right)^2$.\n",
    "\n",
    "Use a list comprehension to collect the sample variance `v_var`. Here's an example to initialize a list with squares of `0` to `9`:\n",
    "```\n",
    "squares = [x**2 for x in range(10)]\n",
    "print(squares)\n",
    "--> [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n",
    "```\n",
    "\n",
    "Why are we plotting $\\sigma(t)$ rather than the $\\text{Var}(t)$? What are the units of each and the units of $\\left\\langle V(t)\\right\\rangle$?\n",
    "\n",
    "**Suggestions**\n",
    "* At each timestep:\n",
    "  * Compute and store in `v_mean` the sample mean $\\left\\langle V(t)\\right\\rangle$\n",
    "  * Initialize a list `v_var_n` with the contribution of each $V_n(t)$ to $\\text{Var}\\left(t\\right)$ with a list comprehension over values of `v_n`\n",
    "  * Compute sample variance `v_var` by summing the values of `v_var_n` with `sum` and dividing by `n-1`\n",
    "  * (alternative: loop over the values of `v_n` and add to `v_var` each contribution $V_n(t)$ and divide by `n-1` outside the loop)\n",
    "  * Compute the standard deviation `v_std` with the function `np.sqrt`\n",
    "  * Plot $\\left\\langle V(t)\\right\\rangle\\pm\\sigma(t)$ with `alpha=0.8` and argument `'C7.'`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 430
    },
    "colab_type": "code",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:13.449063Z",
     "iopub.status.busy": "2020-10-15T15:23:13.448310Z",
     "iopub.status.idle": "2020-10-15T15:23:13.583617Z",
     "shell.execute_reply": "2020-10-15T15:23:13.583987Z"
    },
    "outputId": "cdb70724-657d-46cd-b4e4-dd76db5f8984"
   },
   "outputs": [],
   "source": [
    "# set random number generator\n",
    "np.random.seed(2020)\n",
    "\n",
    "# initialize step_end, n and v_n\n",
    "step_end = int(t_max / dt)\n",
    "n = 50\n",
    "v_n = [el] * n\n",
    "\n",
    "# initialize the figure\n",
    "plt.figure()\n",
    "plt.title('Multiple realizations of $V_m$')\n",
    "plt.xlabel('time (s)')\n",
    "plt.ylabel('$V_m$ (V)')\n",
    "\n",
    "# loop for step_end steps\n",
    "for step in range(step_end):\n",
    "  t = step * dt\n",
    "\n",
    "  v_mean = ...\n",
    "  v_var_n = ...\n",
    "  v_var = ...\n",
    "  v_std = ...\n",
    "\n",
    "  # Complete these lines and uncomment\n",
    "  # plt.plot(...)\n",
    "  # plt.plot(...)\n",
    "  # plt.plot(...)\n",
    "  # plt.plot(...)\n",
    "\n",
    "  for j in range(0, n):\n",
    "    i = ...\n",
    "    v_n[j] = ...\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 430
    },
    "colab_type": "text",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:13.601836Z",
     "iopub.status.busy": "2020-10-15T15:23:13.601284Z",
     "iopub.status.idle": "2020-10-15T15:23:14.332536Z",
     "shell.execute_reply": "2020-10-15T15:23:14.332885Z"
    },
    "outputId": "2623824f-9e23-478a-ebb7-63cc22d6eec6"
   },
   "source": [
    "[*Click for solution*](https://github.com/NeoNeuron/professional-workshop-3/tree/master//tutorials/W0_PythonWorkshop/solutions/W0_Tutorial1_Solution_9e048e4b.py)\n",
    "\n",
    "*Example output:*\n",
    "\n",
    "<img alt='Solution hint' align='left' width=558 height=413 src=https://raw.githubusercontent.com/NeoNeuron/professional-workshop-3/master/tutorials/W0_PythonWorkshop/static/W0_Tutorial1_Solution_9e048e4b_0.png>\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "---\n",
    "## Using NumPy\n",
    "The next set of exercises introduces `np.array`, the workhorse from the scientific computation package [NumPy](https://numpy.org). Numpy arrays the default for numerical data storage and computation and will separate computing steps from plotting.\n",
    "\n",
    "![NumPy package](https://github.com/mpbrigham/colaboratory-figures/raw/master/nma/python-for-nma/numpy_logo_small.png)\n",
    "\n",
    "We updated plots inside the main loop in the previous exercises and stored intermediate results in lists for plotting them. The purpose was to simplify earlier exercises as much as possible. However, there are very few scenarios where this technique is necessary, and you should avoid it in the future. Using numpy arrays will significantly simplify our coding narrative by computing inside the main loop and plotting afterward.\n",
    "\n",
    "Lists are much more natural for storing data for other purposes than computation. For example, lists are handy for storing numerical indexes and text."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "### Exercise 11\n",
    "Rewrite the single neuron plot with random input from _Exercise 7_ with numpy arrays. The time range, voltage values, and synaptic current are initialized or pre-computed as numpy arrays before numerical integration.\n",
    "\n",
    "**Suggestions**\n",
    "* Use `np.linspace` to initialize a numpy array `t_range` with `num=step_end=100` values from `0` to `t_max`\n",
    "* Use `np.ones` to initialize a numpy array `v` with `step_end` leak potential values `el`\n",
    "* Pre-compute `step_end` synaptic current values in numpy array `syn` with `np.random.random(step_end)` for `step_end` random numbers\n",
    "* Iterate for numerical integration of `v`\n",
    "* Since `v[0]=el`, we should iterate for `step_end - 1` steps, for example by skipping `step=0`. Why?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 430
    },
    "colab_type": "code",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:14.351329Z",
     "iopub.status.busy": "2020-10-15T15:23:14.350705Z",
     "iopub.status.idle": "2020-10-15T15:23:14.483136Z",
     "shell.execute_reply": "2020-10-15T15:23:14.483495Z"
    },
    "outputId": "024587d3-9ff8-4812-c844-22d81d551b0d"
   },
   "outputs": [],
   "source": [
    "# set random number generator\n",
    "np.random.seed(2020)\n",
    "\n",
    "# initialize step_end, t_range, v and syn\n",
    "step_end = int(t_max / dt)\n",
    "t_range = np.linspace(0, t_max, num=step_end)\n",
    "v = el * np.ones(step_end)\n",
    "syn = ...\n",
    "\n",
    "# loop for step_end - 1 steps\n",
    "# Complete these lines and uncomment\n",
    "# for step in range(1, step_end):\n",
    "  # v[step] = ...\n",
    "\n",
    "\n",
    "plt.figure()\n",
    "plt.title('$V_m$ with random I(t)')\n",
    "plt.xlabel('time (s)')\n",
    "plt.ylabel('$V_m$ (V)')\n",
    "\n",
    "plt.plot(t_range, v, 'k.')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 433
    },
    "colab_type": "text",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:14.500578Z",
     "iopub.status.busy": "2020-10-15T15:23:14.495981Z",
     "iopub.status.idle": "2020-10-15T15:23:14.754661Z",
     "shell.execute_reply": "2020-10-15T15:23:14.755017Z"
    },
    "outputId": "2c445a4f-4c8f-4cfb-e48f-88015ac66730"
   },
   "source": [
    "[*Click for solution*](https://github.com/NeoNeuron/professional-workshop-3/tree/master//tutorials/W0_PythonWorkshop/solutions/W0_Tutorial1_Solution_aedda11d.py)\n",
    "\n",
    "*Example output:*\n",
    "\n",
    "<img alt='Solution hint' align='left' width=560 height=416 src=https://raw.githubusercontent.com/NeoNeuron/professional-workshop-3/master/tutorials/W0_PythonWorkshop/static/W0_Tutorial1_Solution_aedda11d_0.png>\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "### Exercise 12\n",
    "Let's practice using `enumerate` to iterate over the indexes and values of the synaptic current array `syn`.\n",
    "\n",
    "**Suggestions**\n",
    "* Iterate indexes and values of `syn` with `enumerate` in the `for` loop\n",
    "* Plot `v` with argument `'k'` for displaying a line instead of dots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 430
    },
    "colab_type": "code",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:14.768344Z",
     "iopub.status.busy": "2020-10-15T15:23:14.767098Z",
     "iopub.status.idle": "2020-10-15T15:23:14.902460Z",
     "shell.execute_reply": "2020-10-15T15:23:14.902882Z"
    },
    "outputId": "c2ee0ee0-2952-4f83-f91d-e4484924075c"
   },
   "outputs": [],
   "source": [
    "# set random number generator\n",
    "np.random.seed(2020)\n",
    "\n",
    "# initialize step_end, t_range, v and syn\n",
    "step_end = int(t_max / dt)\n",
    "t_range = np.linspace(0, t_max, num=step_end)\n",
    "v = el * np.ones(step_end)\n",
    "syn = i_mean * (1 + 0.1 * (t_max / dt)**(0.5) * (2 * np.random.random(step_end) - 1))\n",
    "\n",
    "# loop for step_end values of syn\n",
    "for step, i in enumerate(syn):\n",
    "  # skip first iteration\n",
    "  if step==0:\n",
    "    continue\n",
    "  # Complete this line and uncomment\n",
    "  # v[step] = ...\n",
    "\n",
    "plt.figure()\n",
    "plt.title('$V_m$ with random I(t)')\n",
    "plt.xlabel('time (s)')\n",
    "plt.ylabel('$V_m$ (V)')\n",
    "\n",
    "plt.plot(t_range, v, 'k')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 433
    },
    "colab_type": "text",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:14.928589Z",
     "iopub.status.busy": "2020-10-15T15:23:14.926773Z",
     "iopub.status.idle": "2020-10-15T15:23:15.090153Z",
     "shell.execute_reply": "2020-10-15T15:23:15.090501Z"
    },
    "outputId": "9acbee18-9718-4e7a-af12-22051e297c81"
   },
   "source": [
    "[*Click for solution*](https://github.com/NeoNeuron/professional-workshop-3/tree/master//tutorials/W0_PythonWorkshop/solutions/W0_Tutorial1_Solution_4139f63a.py)\n",
    "\n",
    "*Example output:*\n",
    "\n",
    "<img alt='Solution hint' align='left' width=560 height=416 src=https://raw.githubusercontent.com/NeoNeuron/professional-workshop-3/master/tutorials/W0_PythonWorkshop/static/W0_Tutorial1_Solution_4139f63a_0.png>\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "### Exercise 13\n",
    "Plot multiple realizations ($N=50$) of $V(t)$ by storing the voltage of each neuron at time $t$ in a numpy array.\n",
    "\n",
    "**Suggestions**\n",
    "* Initialize a numpy array `v_n` of shape `(n, step_end)` with membrane leak potential values `el`\n",
    "* Pre-compute synaptic current values in numpy array `syn` of shape `(n, step_end)`\n",
    "* Iterate `step_end` steps with a `for` loop for numerical integration\n",
    "* Plot results with a single plot command, by providing `v_n.T` to the plot function. `v_n.T` is the transposed version of `v_n` (with rows and columns swapped)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 430
    },
    "colab_type": "code",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:15.101792Z",
     "iopub.status.busy": "2020-10-15T15:23:15.101290Z",
     "iopub.status.idle": "2020-10-15T15:23:15.221638Z",
     "shell.execute_reply": "2020-10-15T15:23:15.221996Z"
    },
    "outputId": "318d0a09-29c5-4b79-8f9d-73ad67ec44ea"
   },
   "outputs": [],
   "source": [
    "# set random number generator\n",
    "np.random.seed(2020)\n",
    "\n",
    "# initialize step_end, n, t_range, v and syn\n",
    "step_end = int(t_max / dt)\n",
    "n = 50\n",
    "t_range = np.linspace(0, t_max, num=step_end)\n",
    "v_n = el * np.ones([n, step_end])\n",
    "syn = ...\n",
    "\n",
    "# loop for step_end - 1 steps\n",
    "# Complete these lines and uncomment\n",
    "# for step in range(1, step_end):\n",
    "  # v_n[:, step] = ...\n",
    "\n",
    "# initialize the figure\n",
    "plt.figure()\n",
    "plt.title('Multiple realizations of $V_m$')\n",
    "plt.xlabel('time (s)')\n",
    "plt.ylabel('$V_m$ (V)')\n",
    "\n",
    "# Complete this line and uncomment\n",
    "# plt.plot(...)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 433
    },
    "colab_type": "text",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:15.227864Z",
     "iopub.status.busy": "2020-10-15T15:23:15.227364Z",
     "iopub.status.idle": "2020-10-15T15:23:15.569098Z",
     "shell.execute_reply": "2020-10-15T15:23:15.569453Z"
    },
    "outputId": "e1e8cb2c-8a25-483a-97d2-1a694949304c"
   },
   "source": [
    "[*Click for solution*](https://github.com/NeoNeuron/professional-workshop-3/tree/master//tutorials/W0_PythonWorkshop/solutions/W0_Tutorial1_Solution_e8466b6b.py)\n",
    "\n",
    "*Example output:*\n",
    "\n",
    "<img alt='Solution hint' align='left' width=560 height=416 src=https://raw.githubusercontent.com/NeoNeuron/professional-workshop-3/master/tutorials/W0_PythonWorkshop/static/W0_Tutorial1_Solution_e8466b6b_0.png>\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "### Exercise 14\n",
    "Add sample mean $\\left\\langle V(t)\\right\\rangle$ and standard deviation $\\sigma(t)\\equiv\\sqrt{\\text{Var}\\left(t\\right)}$ to the plot.\n",
    "\n",
    "`np.mean(v_n, axis=0)` computes mean over rows, i.e. mean for each neuron\n",
    "\n",
    "`np.mean(v_n, axis=1)` computes mean over columns (axis `1`), i.e. mean for each time step\n",
    "\n",
    "**Suggestions**\n",
    "* Use `np.mean` and `np.std` with `axis=0` to sum over neurons\n",
    "* Use `label` argument in `plt.plot` to specify labels in each trace. Label only the last voltage trace to avoid labeling all `N` of them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 430
    },
    "colab_type": "code",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:15.576209Z",
     "iopub.status.busy": "2020-10-15T15:23:15.575332Z",
     "iopub.status.idle": "2020-10-15T15:23:15.808201Z",
     "shell.execute_reply": "2020-10-15T15:23:15.808553Z"
    },
    "outputId": "4a75c765-ca92-4838-f30f-1d7042bdcd50"
   },
   "outputs": [],
   "source": [
    "# set random number generator\n",
    "np.random.seed(2020)\n",
    "\n",
    "# initialize step_end, n, t_range, v and syn\n",
    "step_end = int(t_max / dt)\n",
    "n = 50\n",
    "t_range = np.linspace(0, t_max, num=step_end)\n",
    "v_n = el * np.ones([n, step_end])\n",
    "syn = i_mean * (1 + 0.1 * (t_max / dt)**(0.5) * (2 * np.random.random([n, step_end]) - 1))\n",
    "\n",
    "# loop for step_end - 1 steps\n",
    "for step in range(1, step_end):\n",
    "  v_n[:,step] = v_n[:,step - 1] + (dt / tau) * (el - v_n[:, step - 1] + r * syn[:, step])\n",
    "\n",
    "v_mean = ...\n",
    "v_std = ...\n",
    "\n",
    "# initialize the figure\n",
    "plt.figure()\n",
    "plt.title('Multiple realizations of $V_m$')\n",
    "plt.xlabel('time (s)')\n",
    "plt.ylabel('$V_m$ (V)')\n",
    "\n",
    "plt.plot(t_range, v_n[:-1].T, 'k', alpha=0.3)\n",
    "\n",
    "# Complete these lines and uncomment\n",
    "# plt.plot(t_range, v_n[-1], 'k', alpha=0.3, label=...)\n",
    "# plt.plot(t_range, ..., 'C0', alpha=0.8, label='mean')\n",
    "# plt.plot(t_range, ..., 'C7', alpha=0.8)\n",
    "# plt.plot(t_range, ..., 'C7', alpha=0.8, label=...)\n",
    "\n",
    "#plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 433
    },
    "colab_type": "text",
    "execution": {
     "iopub.execute_input": "2020-10-15T15:23:15.816515Z",
     "iopub.status.busy": "2020-10-15T15:23:15.816014Z",
     "iopub.status.idle": "2020-10-15T15:23:16.252493Z",
     "shell.execute_reply": "2020-10-15T15:23:16.252848Z"
    },
    "outputId": "ad1612ef-f05f-42a2-90c4-e3a01d986c33"
   },
   "source": [
    "[*Click for solution*](https://github.com/NeoNeuron/professional-workshop-3/tree/master//tutorials/W0_PythonWorkshop/solutions/W0_Tutorial1_Solution_061d112f.py)\n",
    "\n",
    "*Example output:*\n",
    "\n",
    "<img alt='Solution hint' align='left' width=560 height=416 src=https://raw.githubusercontent.com/NeoNeuron/professional-workshop-3/master/tutorials/W0_PythonWorkshop/static/W0_Tutorial1_Solution_061d112f_0.png>\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "include_colab_link": true,
   "name": "W0_Tutorial1",
   "provenance": [],
   "toc_visible": true
  },
  "kernel": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
